package com.nerve.android.cloudoffice.app.activity.notify;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.android.ui.dialog.ConfirmDialog;
import org.nerve.util.DateUtil;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.activity.AutoLoginActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Notify;
import com.nerve.android.cloudoffice.service.data.NotifyData;

/**
 * @project: Cloudoffice
 * @file: NofityActivity.java
 * @package: com.nerve.android.cloudoffice.app.activity.notify
 * @description:
 *
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 15, 2013
 * @change log:
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_notify)
public class NotifyActivity extends NavigationActivity{

	@ViewOnId(id=R.id.message_empty)
	private TextView emptyView;
	@ViewOnId(id=R.id.notifyList)
	private ListView listView;
	
	private NotifyAdapter adapter;
	
	private List<Notify> notifyList = new ArrayList<Notify>();
	private int page;
	private int totalCount;
	
	private ConfirmDialog dialog;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			hideProgressDialog();
			switch(msg.what){
			case WORKER_OK:
				adapter.notifyDataSetChanged();
				saveLastNotifyId();
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("通知公告");
		customNavigateRightButton("新建公告", true);
		
		adapter = new NotifyAdapter();
		
		System.out.println(listView);
		listView.setEmptyView(emptyView);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				read(arg2);
			}
		});
		
		page = 1;
	}
	
	@Override
	protected void initData() {
	}
	
	private void saveLastNotifyId(){
		if(notifyList.size() > 0){
			SharedPreferences sp = getSharedPreferences(AutoLoginActivity.SHARED_FILE, MODE_PRIVATE);
			Editor ed = sp.edit();
			ed.putInt(Const.LAST_NOTIFY_ID, notifyList.get(0).getId());
			ed.commit();
			
			Const.log("保存了的notifyid==== " + notifyList.get(0).getId());
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		refreshData();
	}
	
	private void refreshData(){
		showProgressDialog(null, "正在加载数据，请稍候...");
		
		new NofityWorker().execute();
	}
	
	private void read(int index){
		Notify n = notifyList.get(index);
		
		dialog = null;
		dialog = new ConfirmDialog(this, n.getTitle(), n.getMessage());
		
		dialog.show();
	}
	
	@Override
	protected void onNavigateRightButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.next();
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.changeTab(IndexGroupActivity.HOME);
	}
	
	/**
	 * @project: Cloudoffice
	 * @file: NofityActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity.notify
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 15, 2013
	 * @change log:
	 */
	private class NotifyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return notifyList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return notifyList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			Notify n = notifyList.get(arg0);
			if(arg1 == null)
				arg1 = View.inflate(NotifyActivity.this, R.layout.activity_notify_listitem, null);
			
			((TextView)arg1.findViewById(R.id.title)).setText(n.getTitle());
			((TextView)arg1.findViewById(R.id.content)).setText(n.getMessage());
			
			((TextView)arg1.findViewById(R.id.date))
				.setText(DateUtil.toString(n.getJoinDate(), Const.DATE_FORMAT));
			
			return arg1;
		}
		
	}
	
	private class NofityWorker extends AsyncTask<Void, Void, Integer>{

		@SuppressWarnings("unchecked")
		@Override
		protected Integer doInBackground(Void... params) {
			Reply re = NotifyData.list(page);
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			notifyList = (List<Notify>)re.getMessage();
			if(notifyList == null)
				notifyList = new ArrayList<Notify>();
			try{
				totalCount = Integer.valueOf(re.getData().toString());
				Const.log("notify 加载完成，totalCount = " + totalCount);
			}catch(Exception e){
				e.printStackTrace(System.out);
			}
			return WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}